<?php
/* ******************************
 * Overhead Out aJax actions
 * ******************************
 */

include('../../includes/classes/core.php');

$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error		= '';
$data		= '';

switch ($action) {
	case 'get_edit_page':
		$id		= $_REQUEST['id'];
		$page	= GetPage($id);

		$data 		= array('page'	=> $page);

		break;
    case 'get_out_list':
	    $count	= $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];

		$data = array(
			"aaData"	=> array()
		);

		$rResult = mysql_query("SELECT		`invoice`.`id`,
											`invoice`.`serial_number`,
											`client`.`name`,
											`invoice`.`start_date`,
											SUM( `invoice_detail`.`tax` ),
											CONCAT(\"<a style='padding: 2px' onclick='leter(\",`invoice`.`id`,\")' class='menu ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'>დეტალურად</a>\"),
											`invoice_status`.`name`
								FROM		`invoice`
								LEFT JOIN	`invoice_status` ON `invoice_status`.`id` = `invoice`.`status`
								LEFT JOIN	`client` ON `client`.`rs_un_id` = `invoice`.`buyer_un_id` AND client.actived=1
								LEFT JOIN	`invoice_detail` ON `invoice_detail`.`invoice_id` = `invoice`.`id`
								WHERE invoice.actived=1
								GROUP BY	`invoice`.`id`");

		if(!$rResult){
		    $error = 'Invalid query: ' . mysql_error();
		}else{
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					$row[] = $aRow[$i];
					if($i == ($count - 1)){
						$row[] = '<input type="checkbox" name="check_' . $aRow[0] . '" class="check" value="' . $aRow[0] . '" />';
					}

				}

				$data['aaData'][] = $row;
			}
		}

		break;
	case 'get_in_list':
		$count	= $_REQUEST['count'];
		$hidden = $_REQUEST['hidden'];

		$data = array(
				"aaData"	=> array()
		);/*

		if(!$rResult){
			$error = 'Invalid query: ' . mysql_error();
		}else{
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					$row[] = $aRow[$i];

				}
				$data['aaData'][] = $row;
			}
		}*/

		break;
	case 'disable':
		$id		= $_REQUEST['id'];
	mysql_query("UPDATE 		a_letter
					set         a_letter.invoice_id=NULL
				  WHERE 	    a_letter.invoice_id=$id");
	mysql_query("UPDATE 		d_letter
					set         d_letter.invoice_id=NULL
				  WHERE 		d_letter.invoice_id=$id");
	mysql_query("UPDATE 		invoice
				    set 		invoice.actived=0
				  WHERE 		invoice.id=$id");
		break;
	default:
		$error = "Action is Null";
}

$data['error'] = $error;

echo json_encode($data);

function GetPage($id){
	$data .= '
		<div id="dialog-form">
			<fieldset>
				<legend>გაცემული ანგარიშ–ფაქტურები</legend>
			    <div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area">
				        	</div>
				            <div id="dynamic">
								<table class="display" id="goods_list">
				                    <thead>
				                        <tr id="datatable_header">
					                        <th>ID</th>
				                            <th style="width: 100%">საქონელი / მომსახურება</th>
				                            <th style="width: 120px">ზომის<br/>ერთეული</th>
				                            <th style="width: 120px">რაოდ.</th>
				                            <th style="width: 140px">ღირებულება<br/>დღგ და აქციზის<br/> ჩათვლით</th>
				                            <th style="width: 120px">დაბეგვრა</th>
				                            <th style="width: 105px">დღგ</th>
				                        </tr>
				                    </thead>
				                    <thead>
				                        <tr class="search_header">
				                            <th class="colum_hidden">
				                            	<input type="text"  name="search_id" value="ფილტრი" class="search_init" />
				                            </th>
				                            <th>
							                    <input type="text" name="search_name" value="ფილტრი" class="search_init" />
				                            </th>
				                            <th></th>
				                            <th></th>
							                <th></th>
				                            <th></th>
				                            <th></th>
				                        </tr>
				                    </thead>
	                                <tfoot>
		                        <tr>
		                            <th>&nbsp;</th>
		                            <th>&nbsp;</th>
		                            <th>&nbsp;</th>
		                            <th>&nbsp;</th>
		                            <th>&nbsp;</th>
		                            <th>&nbsp;</th>
		                             <th>&nbsp;</th>
		                        </tr>
                    </tfoot>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
			</fieldset>
	    </div>
		<div class="inner-table" >
			<input type="hidden" id="invoice_id" value="' . $id . '" />
		</div>
	';

	return $data;
}